var goodcat;
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'app/goodcoupon/list',
        datatype: "json",
        colModel: [			
            { label: '商品名称', name: 'title',  width: 80 }, 		 	
            { label: '类型', name: 'typeName',  width: 80 }, 			
			{ label: '商品价格', name: 'zkFinalPrice',  width: 80 }, 			
			{ label: '剩余优惠券', name: 'couponRemainCount',  width: 80 }, 			
			{ label: '商品图片', name: 'pictUrl',  width: 80,formatter: function(value, options, row){
				return '<img width="110" height="110" src="'+value+'"/>';
			} }, 			
			{ label: '详情', name: 'couponInfo',  width: 80 }, 			
			{ label: '优惠券额度', name: 'couponPrice',  width: 80 }, 			
			{ label: '优惠券地址', name: 'couponClickUrl', width: 80,hidden:true }, 			
			{ label: '商品地址', name: 'itemUrl',  width: 80,hidden:true  }, 			
			{ label: '佣金率', name: 'commissionRate',width: 80 }, 			
			{ label: '开始时间', name: 'couponStartTime',  width: 80,hidden:true  }, 			
			{ label: '结束时间', name: 'couponEndTime',  width: 80 }			
        ],
        autowidth:true,
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			goodName: null,
			cat:null
		},
		showList: true,
		title: null,
		goodCoupon: {},
		sysCodeList:goodcat
	},
	created: function () {
			$.get(baseURL + "sys/syscode/listgoodcat", function(r){
	            vm.sysCodeList = r.sysCodeList;
	        });
		  },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.goodCoupon = {};
			vm.formatDate();
			vm.getCode();
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.formatDate();
            vm.getInfo(id);
            vm.getCode();
		},
		saveOrUpdate: function (event) {
			var url = vm.goodCoupon.id == null ? "app/goodcoupon/save" : "app/goodcoupon/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.goodCoupon),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "app/goodcoupon/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "app/goodcoupon/info/"+id, function(r){
                vm.goodCoupon = r.goodCoupon;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'title': vm.q.goodName,'category': vm.q.cat},
                page:page
            }).trigger("reloadGrid");
		},
		formatDate:function(){
			$('.form_datetime').datetimepicker({
		        weekStart: 1,
		        todayBtn:  1,
				autoclose: 1,
				todayHighlight: 1,
				startView: 2,
				forceParse: 0,
		        showMeridian: 1,
		        format: "yyyy-mm-dd HH:ii:ss",
		        language:  'zh-CN'
		    });
		},
		getCode: function(){
			$.get(baseURL + "sys/syscode/listgoodcat", function(r){
                vm.sysCodeList = r.sysCodeList;
            });
		},
		importGood:function(){

            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "输入商品开始结束位置",
                area: ['300px', '200px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#showIndex"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                	var indexx = layer.load(0, {shade: false}); 
                	$.ajax({
    					type: "POST",
    				    url: baseURL + "app/goodcoupon/importGood",
    				    data: {start:$("#indexstart").val(),end:$("#indexend").val()},
    				    success: function(r){
    				    	 layer.close(indexx);
    						if(r.code == 0){
    							alert('导入成功', function(index){
    								$("#jqGrid").trigger("reloadGrid");
    							});
    						}else{
    							alert(r.msg);
    						}
    					}
    				});
                    layer.close(index);
                }
            });
        
		}
	}
});